package com.example.demo.config;


import com.example.demo.intercept.AdminIntercept;
import com.example.demo.intercept.TimeStampIntercept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class WebConfiguration extends WebMvcConfigurationSupport {

    @Autowired
    private TimeStampIntercept timeStampIntercept;
    @Autowired
    private AdminIntercept adminIntercept;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {


        InterceptorRegistration registration2 = registry.addInterceptor(adminIntercept);
        //所有路径都被拦截,验证token
        registration2.addPathPatterns("/author/**");
        registration2.excludePathPatterns("/doc.html*/**", "/error/**", "/webjars/**", "/swagger-resources/**",
                "/v2/**", "/v3/**", "/static/**", "/favicon*", "/author/userRegist/**", "/author/dologin/**", "/author/userVerify/**"

        );


        super.addInterceptors(registry);
    }


    @Override
    /**
     * 修改springboot中默认的静态文件路径
     */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //addResourceHandler请求路径
        //addResourceLocations 在项目中的资源路径
        //setCacheControl 设置静态资源缓存时间
        // registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/**")
                .addResourceLocations(
                        "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html", "doc.html")
                .addResourceLocations(
                        "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations(
                        "classpath:/META-INF/resources/webjars/");

    }
}
